package cn.crex1.xiaomi.core.mvc.context.web;

import javax.servlet.ServletContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.crex1.xiaomi.core.mvc.beans.BeanFactory;
import cn.crex1.xiaomi.core.mvc.context.AbstractWebContext;

public class ConfigWebContext extends AbstractWebContext {

	private static final String CONTEXTID = "contextid";

	private static final String CONTEXTNAME = "xiaomi";

	private static final String CONTEXT = "context";

	private static Logger logger = LoggerFactory.getLogger(ConfigWebContext.class);

	private ServletContext context;

	public ConfigWebContext() {
		super();

	}

	public ConfigWebContext(ServletContext context) {
		super();
		this.context = context;
	}

	public void setContext(ServletContext context) {
		this.context = context;
	}

	@Override
	public void start() {
		if (!isclose()) {
			context.setAttribute(CONTEXTID, getContextid());
			context.setAttribute(CONTEXTNAME, getContextname());
			context.setAttribute(CONTEXT, context);
		}
	}

	@Override
	public void stop() {
		if (!isclose()) {
			context.removeAttribute(CONTEXTID);
			context.removeAttribute(CONTEXTNAME);
			context.removeAttribute(CONTEXT);
		} else if (context == null) {
			try {
				throw new Exception("Webcontext Not starting");
			} catch (Exception e) {
				e.printStackTrace();
			}
			logger.error("Webcontext Not starting");
		}
	}

	@Override
	public void refresh() {
		stop();
		start();
	}

	@Override
	public ServletContext getServletContext() {
		return context;
	}

	@Override
	public boolean isclose() {
		if (context != null) {
			if (context.getAttribute(CONTEXT) != null) {
				return false;
			}
		}
		return true;
	}

	@Override
	public BeanFactory getAutowireBeanfactory() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object getBean(String name) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Class<?> getType(String name) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean containsBean(String name) throws Exception {
		// TODO Auto-generated method stub
		return false;
	}
}
